package com.max.project.graphics.task;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.max.common.proxy.ServiceProxyFactory;
import com.max.common.util.DataPage;
import com.max.common.util.JsonUtil;
import com.max.common.util.StringUtil;
import com.max.project.graphics.model.Content;
import com.max.project.graphics.service.ContentService;
import com.max.project.graphics.service.MicroBlogTencentService;

/**
 * 
 * 项目名称:max-project-graphics
 * 文件名称:MicroBlogTencentTask.java
 * 文件描述:
 * 创建人员:chenyibo
 * 创建时间:2013-1-29 下午03:14:33
 * 修改人员:chenyibo
 * 修改时间:2013-1-29 下午03:14:33
 * 修改备注:
 * @version V1.0
 */
public class MicroBlogTencentTask implements Runnable
{

	private static ContentService contentService = 
		new ServiceProxyFactory().create(ContentService.class);
	
	private MicroBlogTencentService microBlogTencentService = 
		new ServiceProxyFactory().create(MicroBlogTencentService.class);
	
	@Override
	public void run() 
	{
		try
		{
			int hasnext = 0;
			long lastid = 0L;
			long pagetime = 0L;
			List<Content> contentList = new ArrayList<Content>();
			
			Content lastContent = null;
			DataPage<Content> dp = contentService.query(1, 1);
			if(dp != null && dp.getList() != null && !dp.getList().isEmpty())
				lastContent = dp.getList().get(0);
			
			//1.请求最新
			while(hasnext == 0)
			{
				JSONObject json = microBlogTencentService.statuses_user_timeline("meiyu0820", 50, lastid, pagetime, 1);
				int ret = JsonUtil.getValue(json, "ret", 0);
				
				if(ret==0 && JsonUtil.getJSONObject(json, "data") != null
						&& JsonUtil.getJSONArray(json, "data.info") != null)
				{
					hasnext = JsonUtil.getValue(json, "data.hasnext", 1);
					
					JSONArray infos = JsonUtil.getJSONArray(json, "data.info");
					for(int i=0;i<infos.length();i++)
					{
						JSONObject info = infos.getJSONObject(i);
						if(info != null)
						{
							String origtext = JsonUtil.getValue(info, "origtext", "").trim();
							JSONArray image = JsonUtil.getJSONArray(info, "image");
							String nick     = JsonUtil.getValue(info, "nick", "");
							lastid          = JsonUtil.getValue(info, "id", 0L);
							pagetime        = JsonUtil.getValue(info, "timestamp", 0);
							Date date = pagetime==0?new Date():new Date(1000L*pagetime);
							
							if(lastContent != null && lastContent.getCreate_date().getTime()>=1000L*pagetime)
							{
								hasnext = 1;
								break;
							}
							
							Content content = new Content();
							content.setName(origtext);
							content.setContent(image==null?"[]":image.toString());
							content.setCreator(nick);
							content.setModifier(nick);
							content.setCreate_date(date);
							content.setModify_date(date);
							
							contentList.add(content);
							System.out.println(content.getName());
							System.out.println(content.getContent());
						}
					}
				}
				else
					hasnext = 1;
			}
			
			
			//2.合并
			Collections.reverse(contentList);
			Iterator<Content> it = contentList.iterator();
			Content prevContent = null;
			Content currContent = null;
			while(it.hasNext())
			{
				currContent = it.next();
				
				if(currContent.getName().matches(".*(!|！)$"))
				{
					//...cover:img,img,img....
					JSONArray currImage = new JSONArray(currContent.getContent());
					if(currImage.length()>0 && !StringUtil.isEmpty(currImage.getString(0)))
						currImage.put(0, "cover:"+currImage.getString(0));
					currContent.setContent(currImage.toString());
					currContent.setName(currContent.getName().replaceAll("(!|！)$", ""));
				}
					
				if(prevContent != null && currContent != null
						&& prevContent.getName().equals(currContent.getName()))
				{
					JSONArray prevImage = new JSONArray(prevContent.getContent());
					JSONArray currImage = new JSONArray(currContent.getContent());
					
					for(int i=0;i<currImage.length();i++)
						prevImage.put(currImage.getString(i));
					
					prevContent.setContent(prevImage.toString());
					it.remove();
				}
				else
					prevContent = currContent;
			}
			
			//3.插入数据库
			System.out.println("============================");
			for(Content content:contentList)
			{
				content.setContent(content.getContent().replaceAll("\\\"", "").replaceAll("\\[", "").replaceAll("\\]", ""));
//				contentService.insert(content);
				System.out.println(content.getName());
				System.out.println(content.getContent());
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args)
	{
		MicroBlogTencentTask task = new MicroBlogTencentTask();
		task.run();
	}
}
